<?php 
namespace app\user\controller;
use app\common\controller\Api;
use think\Request;
use think\Loader;
use app\user\traits\controllers\TraitUser;
/**
 * 心愿订单
 */
class WishOrder extends Api
{
	use TraitUser;
	
	//前置操作
    protected $beforeActionList = [
        'checkLogin'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }

	/**
	 * 心愿订单房车资源详情
	 */
	public function getCaravanDetail()
	{
		$wishInfo = model('Wishs')	->hasWhere('meetWishUsers',['caravan_id'=>$this->request->get('cid/d'), 'wish_id' => $this->request->get('wid/d')])
									->where(['user_id' => Request::instance()->user->id])->find();
		$caravanInfo = model('Caravans')->get(['id'=>$this->request->get('cid/d')]);							
		$days = (strtotime($wishInfo['end_date'])-strtotime($wishInfo['start_date']))/(24*60*60)+1;
		$picCaravan = $this->getUrl($caravanInfo->piccaravan);
        $caravancover= $this->getUrl($caravanInfo->caravancover);
		//房车所在营地旅行基数
		$travel_money_base = model('Camps')->where(['id'=>$caravanInfo['services']])->value('travel_money_base');
		$travel_coin_money = Loader::model('Orders')->calculatePrice($caravanInfo->id,$wishInfo['start_date'],$wishInfo['end_date']);
		$amount = ['travel_coin' => 0, 'total_money' => 0];
		foreach ($travel_coin_money as $k => $v) {
			$amount[$v->types] = $v->amount; 
		}
		return view('wishorder/caravan_detail',['carInfo'         	=> $caravanInfo,
												'wishInfo'        	=> $wishInfo,
												'days'			  	=> $days,
												'picCaravan'      	=> $picCaravan,
												'caravancover'    	=> $caravancover,
												'travel_money_base' => $travel_money_base,
												'amount'			=> $amount
												]);	
	}

	/**
	 * 下订单界面
	 */
	public function getCreateOrder()
	{
		$wishInfo = model('Wishs')	->hasWhere('meetWishUsers',['caravan_id'=>$this->request->get('cid/d'), 'wish_id' => $this->request->get('wid/d')])
									->where(['user_id' => Request::instance()->user->id])->find();
		$caravanInfo = model('Caravans')->get(['id'=>$this->request->get('cid/d')]);	
		//获取联系人信息
		$contacts = model('ContactInfos')->where(['user_id' => Request::instance()->user->id])->select();
		//会员旅行币余额
		$integral = model('Capitals')->getIntegral(Request::instance()->user->id);
		$days = (strtotime($wishInfo['end_date'])-strtotime($wishInfo['start_date']))/(24*60*60)+1;
		$picCaravan = $this->getUrl($caravanInfo->piccaravan);
        $caravancover= $this->getUrl($caravanInfo->caravancover);
		//房车所在营地旅行基数
		$travel_money_base = model('Camps')->where(['id'=>$caravanInfo['services']])->value('travel_money_base');
		$travel_coin_money = Loader::model('Orders')->calculatePrice($caravanInfo->id,$wishInfo['start_date'],$wishInfo['end_date']);
		$amount = ['travel_coin' => 0, 'total_money' => 0];
		foreach ($travel_coin_money as $k => $v) {
			$amount[$v->types] = $v->amount; 
		}
		return view('wishorder/create_order',['carInfo'           	=> $caravanInfo,
												'wishInfo'        	=> $wishInfo,
												'amount'		  	=> $amount,
												'days'			 	 => $days,
												'picCaravan'      	=> $picCaravan,
												'caravancover' 		=> $caravancover,
												'travel_money_base' => $travel_money_base,
												'integral'		  	=> $integral,
												'contacts'		  	=> $contacts
												]);	
	}

	/**
	 * 生成订单
	 */
	public function postSaveOrder(){
		$wishInfo = model('Wishs')	->hasWhere('meetWishUsers',['caravan_id'=>$this->request->post('cid/d'), 'wish_id' => $this->request->post('wid/d')])
									->where(['user_id' => Request::instance()->user->id])->find();
		
		$meet_id = model('meetWishUsers')->where(['caravan_id'=>$this->request->post('cid/d'), 'wish_id'=>$wishInfo->id])->value('id');
		$caravan_info = Loader::model('Caravans') -> with('camps') -> where(['id' => $this->request->post('cid/d')])->find();
		
		if(empty($this->request->post('rent_contact_id/a'))){return $this -> error('请选择联系人', $url=null, $wait = 5);}
        if ($this->request->post('Protocol/b')!==true) {return $this->error('房车营地规则和服务条款',$url = null,'',$wait=5);}
        //图片验证码
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($this->request->post('imgCode'))) {return $this->error(lang('图片验证码错误'),$url = null,'',$wait=5);}
		$other['rent_contact_id'] = $this->request->post('rent_contact_id/a');
		$other['take_car_area'] = $caravan_info->camps->name;
		$other['leave_car_area'] = $caravan_info->camps->name;
		//生成订单
		$result = $this->user->completeWish($wishInfo->id, $meet_id, $other);
		$this->redirect("/wishorders/pay?number={$result->number}");
	}

	/**
	 * 添加联系人
	 */
	public function postAddLinkman(){
		$user_id['user_id'] = Request::instance()->user->id;
	   	$re = Loader::model('ContactInfos')->create(array_merge($this->request->post(),$user_id),true);
	   	if($re){return json(['code'=>1]);}
	}
	
	/**
	 * 支付界面 
	 */
	 public function getPay(){
	 	$number = $this->request->get('number');
        $order_info = Loader::model('Orders')->where('number',$number)->find();
		$linkMan = [];
		//联系人id
        if (!empty($linkman_id = $order_info->rent_contact_id)) {
            foreach ($linkman_id as $v) {
                $list = Loader::model('ContactInfos')->where('id',$v)->find();
                $linkMan[] = $list;
            }
        }
        return view('wishorder/pay',['order_info'=>$order_info, 'linkMan' => $linkMan]);
	}
	 
	 
	 
	/**
	 * 转移路径斜杠
	 */
	public function getUrl($url)
    {
        return $url = str_replace("\\","/",$url);
    }
}







 ?>